
 /**  
 * Project Name:zhonghecrm  
 * File Name:SysDepartmentController.java  
 * Package Name:com.crm.controller  
 * Date:2016年10月24日下午6:54:44   
 *  
*/  
  
package com.crm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.SysDepartment;
import com.crm.model.SysDuty;
import com.crm.model.SysUser;
import com.crm.service.SysDepartmentService;
import com.crm.service.SysDutyService;
import com.crm.util.ResponseUtil;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;

/**  
 * ClassName:SysDepartmentController <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2016年10月24日 下午6:54:44 <br/>  
 * @author   hanlinxiu  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */
@Controller
@RequestMapping("/department")
public class SysDepartmentController {
	
	@Autowired
	private SysDepartmentService sysDepartmentService;
	
	@Autowired
	private SysDutyService sysDutyService;
	
	@RequestMapping("/index")
	public String index(Page<SysDepartment> page, SysDepartment t,ModelMap map){
		/*Page<SysDepartment> list = (Page<SysDepartment>) sysDepartmentService.selectDeparmentAlle(page, t);
		map.put("list", list);*/
		return "sysconf/department1";
	}
	
	//插入新部门
	@RequestMapping("/addDepartement")
	@ResponseBody
	public Map<String, Object> addDepartement(SysDepartment department){
		
		sysDepartmentService.insert(department);
		
		Map<String, Object> map = new HashMap<>();
		map.put("status", "success");
		map.put("msg", "新部门创建成功！");
		return map;  
	}
	//删除部门数据根据department_id
	@RequestMapping("/deleteDepartement")
	@ResponseBody
	public Map<String, Object> deleteDepartement(SysDepartment department){
		sysDepartmentService.updateByPrimaryKey(department);
		Map<String, Object> map = new HashMap<>();
		map.put("status", "success");
		map.put("msg", "删除部门数据成功！");
		return map; 
	}
	//修改部门数据根据department_id
	@RequestMapping("/updateByIdDepartement")
	@ResponseBody
	public Map<String, Object> updateByIdDepartement(SysDepartment department){
		sysDepartmentService.updateByPrimaryKey(department);
		Map<String, Object> map = new HashMap<>();
		map.put("status", "success");
		map.put("msg", "更新部门数据成功！");
		return map; 
	}
	//查询部门数据
	@RequestMapping("/selectAllDepartement")
	@ResponseBody
	public List<SysDepartment> selectAllDepartement(Page<SysDepartment> page){
		List<SysDepartment> findAllList = sysDepartmentService.selectAll(page);
		return findAllList;
	}
	//查询部门数据根据department_id
	@RequestMapping("/selectByIdDepartement")
	@ResponseBody
	public SysDepartment selectByIdDepartement(String id){
		SysDepartment department = sysDepartmentService.selectByPrimaryKey(id);
		return department;
	}
	
	@RequestMapping("/getByWhere")
	@ResponseBody
	public Map<String, Object> getByWhere(Page<SysDepartment> page, SysDepartment t){
		Map<String, Object> map = new HashMap<>();
		
		List<SysDepartment> list = sysDepartmentService.selectAll(page, t);
		
		List<SysDuty> dutyList = new ArrayList<>();
		
		if(!ValidateUtil.isEmpty(t.getParentId())){
			//获取部门下的职位
			dutyList = sysDutyService.getByDepartmentId(t.getParentId());
		}
		
		map.put("dutyList", dutyList);
		map.put("list", list);
		map.put("status", "success");
		return map;
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public void getAll(HttpServletResponse response,HttpServletRequest	 request, Page<SysDepartment> page, SysDepartment t){
		
		Page<SysDepartment> list = (Page<SysDepartment>) sysDepartmentService.selectAll(page, t);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/getAllOption")
	@ResponseBody
	public Map<String, Object> getAllOption(Page<SysDepartment> page, SysDepartment record){
		record.setEnable(1);
		Map<String, Object> map = new HashMap<>();
		Page<SysDepartment> list = (Page<SysDepartment>)sysDepartmentService.selectAll(page, record);
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "获取成功！");	
		return map;
	}
	
	@RequestMapping("/selectDepartementByUser")
	@ResponseBody
	public SysDepartment selectDepartementByUser(){
		SysUser sysUser =  (SysUser)SessionUtils.get(SessionModel.SESSION_USER);
		SysDepartment department = sysDepartmentService.selectByPrimaryKey(sysUser.getDepartmentId());
		if(department==null) return new SysDepartment();
		String fullPath = department.getFullPath();
		if(fullPath==null) return new SysDepartment();
		if("0".equals(fullPath)){
			return department;
		}else{
			department =sysDepartmentService.selectByPrimaryKey(fullPath.split(",")[1]);
			return department;
		}
		
	}

}














  
